package me.zhengjie.minbearwcs.controller.outbound;

import lombok.RequiredArgsConstructor;
import me.zhengjie.minbearwcs.entity.DTO.transfer.Tuple;
import me.zhengjie.minbearwcs.serivce.outbound.OutboundService;
import org.apache.xmlbeans.impl.regex.REUtil;
import org.springframework.web.bind.annotation.*;
import me.zhengjie.minbearwcs.entity.DTO.outbound.wms.WmsSortException;
import me.zhengjie.minbearwcs.entity.DTO.outbound.wms.order.WmsOutboundOrder;
import me.zhengjie.minbearwcs.entity.DTO.response.Resp;

import java.util.Map;

@RestController
@RequiredArgsConstructor
public class OutboundWmsController {

    private final OutboundService outboundService;


    // 出库单下发
    @PostMapping("/outboundOrder/dispatch")
    public Resp outboundOrderDispatch(@RequestBody WmsOutboundOrder wmsOutboundOrder) {
        outboundService.outboundOrderDispatch(wmsOutboundOrder);
        return Resp.build();
        /**
         *
         *  1. 出库工单信息插入出库工单表和明细表
         *  2.
         *
         */

        /**
         * 出库单开始状态为等待开始状态（需要人工点开始）
         * 当接收到订单需要备货后，订单状态切换为等待备货状态
         *
         */

        /**
         * 备货操作
         *  在设置好的备货时间段里执行
         *
         * 1.出库单表获取需要备货，状态为等待备货，优先级数字最小的单
         * 2.获取备货数量 小于 需求数量的明细
         *    找到尾数区里符合的栈板
         *   2.1 先找尾数区装该种物料的栈板，找到则把数量加到明细已备货数量，
         *          如果数量是等于，则把明细状态设置为完成备货
         *   2.2 把栈板信息插入到出库备货表里
         *   2.3 更新栈板表的备货数量
         *
         *     使用待分拣区获取储位策略
         *   2.4 如果找到，则在库区里找到
         *
         */
    }

    // 出库单完成通知
    @PostMapping("/outboundOrder/complete")
    public Resp outboundOrderComplete(@RequestBody Map<String,String> infoReq) {
        String outboundOrderCode = infoReq.get("outboundOrderCode");
        outboundService.outboundOrderComplete(outboundOrderCode);
        return Resp.build();
    }

    // 出库单取消
    @PostMapping("/outbound/cancel")
    public Resp outboundOrderCancel(@RequestBody Map<String,String> infoReq) {
        String outboundOrderCode = infoReq.get("outboundOrderCode");
        Tuple<String> tuple = outboundService.outboundOrderCancel(outboundOrderCode);
        if (tuple.success) {

            return Resp.build();
        } else {
            return Resp.build()
                    .code(false)
                    .reason(tuple.value);
        }
    }

    // 分拣异常通知
    @PostMapping("/sort/exception")
    public Resp sortException(@RequestBody WmsSortException wmsSortException) {
        return Resp.build();
    }

}
